package com.eryos.android.cigarettecounter.tool;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;

/**
 * Project : CigaretteCounter Author : Olivier Combe URL : http://eryos.fr/CigaretteCounter/
 * 
 * Copyright(C)2011 - Olivier Combe
 * 
 * Licensed under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 * 
 * @version \$Revision.*\$
 * @author Olivier Combe
 */
public final class Tools {

    /**
     * Private/Hidden constructor.
     */
    private Tools() {
    }

    /**
     * @param month
     *            number(from 1 to 12)
     * @return the last day of the month in param.
     */
    public static int getLastDayOfMonth(final int month) {

        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, month); // 15/03/20XX
        c.add(Calendar.MONTH, 1); // 15/04/20XX
        c.set(Calendar.DAY_OF_MONTH, 1); // 01/04/20XX
        c.add(Calendar.DAY_OF_MONTH, -1); // 31/03/20XX

        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * @param ctx
     *            Acutal context
     * @param month
     *            to verify
     * @return Last day of the month with data (date format : 23/01/2011).
     */
    public static String getMonthLabel(final Context ctx, final int month) {

        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, month); // 15/03/20XX
        c.set(Calendar.DAY_OF_MONTH, 1); // 01/04/20XX

        SimpleDateFormat sdf = new SimpleDateFormat("MMMMM");
        String monthLabel = sdf.format(c.getTime());
        return monthLabel;

    }

    /**
     * @param ctx
     *            Acutal context
     * @param month
     *            to trunc
     * @return first letters of the Month
     */
    public static String getMonthFirstLetter(final Context ctx, final int month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, month); // 15/03/20XX
        c.set(Calendar.DAY_OF_MONTH, 1); // 01/04/20XX

        SimpleDateFormat sdf = new SimpleDateFormat("MMM");
        String monthLabel = sdf.format(c.getTime());
        return monthLabel;
    }

    /**
     * @param ctx
     *            Acutal context
     * @param day
     *            Day to reformat
     * @return first letters of the day
     */
    public static String getDayFirstLetter(final Context ctx, final int day) {

        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_WEEK, day); // 01/04/20XX

        SimpleDateFormat sdf = new SimpleDateFormat("E"); // Day in Week
        String dayLabel = sdf.format(c.getTime());
        return dayLabel;

    }

    /**
     * Genereate a timestamp.
     * 
     * @return the current timestamp with format : YYYMMDD_HH24Mi
     */
    public static String getTimestamp() {

        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        int hour = c.get(Calendar.HOUR);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);

        // Put month value in range 1-12 instead of 0-11
        month++;

        String sYear = String.valueOf(year);
        String sMonth = String.valueOf(month);
        String sDay = String.valueOf(day);

        String sHour = String.valueOf(hour);
        String sMinute = String.valueOf(minute);
        String sSecond = String.valueOf(second);

        // Add Leading 0
        if (sMonth.length() == 1) {
            sMonth = "0" + sMonth;
        }

        if (sDay.length() == 1) {
            sDay = "0" + sDay;
        }

        if (sHour.length() == 1) {
            sHour = "0" + sHour;
        }

        if (sMinute.length() == 1) {
            sMinute = "0" + sMinute;
        }

        if (sSecond.length() == 1) {
            sSecond = "0" + sSecond;
        }

        return sYear + "" + sMonth + "" + sDay + "_" + sHour + "" + sMinute;
    }

}
